package cn.rkylin.oms.authorization.controller;

import cn.rkylin.oms.area.domain.TopArea;
import cn.rkylin.oms.authorization.service.AuthorizationService;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author 我是谁
 *
 */
@Controller
@RequestMapping("/authorization")
public class AuthorizationController {

    @Autowired
    AuthorizationService authorizationService;

    @Autowired
    LogUtil logUtil;

    /**
     * http://localhost:8080/authorization/authorizationVip/shopId/aaa.action?code=bbb
     * 唯品会直发授权
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/authorizationVip/shopId/{shopId}", method = RequestMethod.GET)
    public Map<String, Object> authorizationVip(@PathVariable String shopId, @RequestParam String code) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            returnMap = authorizationService.authorizationVip(shopId, code);
            logUtil.info(new DBLog(shopId, code, "authorizationVip", JSON.toJSONString(returnMap)));
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("msg", e.getMessage());
            returnMap.put("success", "false");
            logUtil.error(new DBLog(shopId, code, "authorizationVip", e.getMessage()), e);
        }
        return returnMap;
    }

    /**
     * 苏宁授权
     *
     * @param shopId
     * @param scope
     * @param state
     * @param itemcode
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/authorizationSuning/shopId/{shopId}", method = RequestMethod.GET)
    public Map<String, Object> authorizationSuning(@PathVariable String shopId, @RequestParam String code) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        Map<String, String> logMap = new HashMap();
        logMap.put("shopId", shopId);
        logMap.put("code", code);

        try {
            returnMap = authorizationService.authorizationSuning(shopId, code);
            logUtil.info(new DBLog(shopId, JSON.toJSONString(logMap), "authorizationSuning", JSON.toJSONString(returnMap)));
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("msg", e.getMessage());
            returnMap.put("success", "false");
            logUtil.error(new DBLog(shopId, JSON.toJSONString(logMap), "authorizationSuning", e.getMessage()), e);
        }
        return returnMap;
    }

    /**
     * http://localhost:8080/authorization/getAuthorizationUrl.action?shopId=4ba942306c844b218492720914a88d13
     * 获取授权地址
     *
     * @param shopId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAuthorizationUrl", method = RequestMethod.GET)
    public Map<String, Object> getAuthorizationUrl(@RequestParam String shopId) {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            returnMap = authorizationService.getAuthorizationUrl(shopId);
            logUtil.info(new DBLog(shopId, "", "getAuthorizationUrl", JSON.toJSONString(returnMap)));
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("msg", e.getMessage());
            returnMap.put("success", "false");
            logUtil.error(new DBLog(shopId, "", "getAuthorizationUrl", e.getMessage()), e);
        }
        return returnMap;
    }
}
